﻿
Imports System.Data.SqlClient
Imports System.Data
Imports Microsoft.ApplicationBlocks.Data
Imports DQDataTypes = App_Code.DQDataTypes
Imports IUIBuildingBlock = App_Code.IUiBuildingBlock
Imports test = DynamicControls.IUIBuildingBlock


Partial Class EnterData2
    Inherits System.Web.UI.Page
    Private _customerId As Integer
    Private _hotDealId As Integer
    Private _questionId As Integer
    Private _name As String
    Protected Sub PageInit(sender As Object, e As System.EventArgs) Handles Me.Init
        BuildDynamicUi()
    End Sub

    Protected Sub PageLoad(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            customerName.Text = _name

            'Load current dynamic answers
            LoadDynamicAnswers()
        End If

    End Sub
    Private Sub BuildDynamicUi()    'Called from Page_Init
        _customerId = Convert.ToInt32(Request.QueryString("ID"))

        Dim reader As SqlDataReader = SqlHelper.ExecuteReader(ConfigurationSettings.AppSettings("connectionString"), _
                            CommandType.StoredProcedure, "dq_GetCustomerInformation", _
                            New SqlParameter("@CustomerID", _customerId))

        If reader.Read Then
            _hotDealId = Convert.ToInt32(reader("HotDealID"))
            _name = reader("Name").ToString()
        Else
            Throw New ArgumentException("No customer with CustomerID " & _customerId & " was found in the database!")
        End If

        'Get the list of dynamic controls for the specified customer
        reader = SqlHelper.ExecuteReader(ConfigurationSettings.AppSettings("connectionString"), _
                            CommandType.StoredProcedure, "dq_GetDynamicQuestionsForHotDeal", _
                            New SqlParameter("@HotDealID", _hotDealId))

        'For each question, add the necessary user control
        While reader.Read
            'To loading Option question
            _questionId = Convert.ToInt32(reader("QuestionID"))
            'Session("questionId") = _questionId

            Dim dq As UserControl = LoadControl(ResolveUrl(ConfigurationSettings.AppSettings("buildingBlockPath") & reader("ControlSrc")))
            CType(dq, IUIBuildingBlock).QuestionText = reader("QuestionText").ToString()
            CType(dq, IUIBuildingBlock).QuestionId = _questionId
            dq.ID = String.Concat("dq", reader("DynamicQuestionID"))

            dynamicControls.Controls.Add(dq)
            dynamicControls.Controls.Add(New LiteralControl("<br /><br />"))
        End While

        reader.Close()
    End Sub
    Private Sub LoadDynamicAnswers()
        'Get the answers for this customer
        'Get the list of dynamic controls for the specified customer
        Dim reader As SqlDataReader = SqlHelper.ExecuteReader(ConfigurationSettings.AppSettings("connectionString"), _
                            CommandType.StoredProcedure, "dq_GetDynamicAnswersForCustomer", _
                            New SqlParameter("@CustomerID", _customerId))

        While reader.Read
            Dim dq As IUIBuildingBlock = dynamicControls.FindControl(String.Concat("dq", reader("DynamicQuestionID")))
            If Not dq Is Nothing Then
                Select Case dq.DataType
                    Case DQDataTypes.String
                        dq.Answer = reader("StringAnswer").ToString()
                    Case DQDataTypes.Integer
                        dq.Answer = Convert.ToInt32(reader("IntegerAnswer"))
                    Case DQDataTypes.Double
                        dq.Answer = Convert.ToSingle(reader("DoubleAnswer"))
                    Case DQDataTypes.Date
                        dq.Answer = Convert.ToDateTime(reader("DateAnswer"))
                    Case DQDataTypes.Currency
                        dq.Answer = Convert.ToDecimal(reader("CurrencyAnswer"))
                    Case DQDataTypes.Boolean
                        dq.Answer = Convert.ToBoolean(reader("BooleanAnswer"))
                    Case DQDataTypes.Option
                        dq.Answer = Convert.ToInt32(reader("OptionAnswer"))
                End Select
            End If
        End While

        reader.Close()
    End Sub


    Protected Sub BtnCancelClick(sender As Object, e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect("Default.aspx")
    End Sub

    Protected Sub btnSaveValues_Click(sender As Object, e As System.EventArgs) Handles btnSaveValues.Click
        If Not Page.IsValid Then Exit Sub

        'Create the needed parameters
        Dim stringParam As New SqlParameter("@StringAnswer", SqlDbType.NText)
        Dim integerParam As New SqlParameter("@IntegerAnswer", SqlDbType.Int)
        Dim doubleParam As New SqlParameter("@DoubleAnswer", SqlDbType.Decimal)
        Dim dateParam As New SqlParameter("@DateAnswer", SqlDbType.DateTime)
        Dim currencyParam As New SqlParameter("@CurrencyAnswer", SqlDbType.Money)
        Dim booleanParam As New SqlParameter("@BooleanAnswer", SqlDbType.Bit)
        Dim optionParam As New SqlParameter("@OptionAnswer", SqlDbType.Int)

        'Enumerate each answer and save it back to the database
        For Each c As Control In dynamicControls.Controls
            If TypeOf c Is IUIBuildingBlock Then
                'Mark all of the parameters as NULL
                stringParam.Value = DBNull.Value : integerParam.Value = DBNull.Value
                doubleParam.Value = DBNull.Value : dateParam.Value = DBNull.Value
                currencyParam.Value = DBNull.Value : booleanParam.Value = DBNull.Value
                optionParam.Value = DBNull.Value

                'Determine which parameter needs to be set
                Dim uib As IUIBuildingBlock = CType(c, IUIBuildingBlock)
                Select Case uib.DataType
                    Case DQDataTypes.String
                        stringParam.Value = uib.Answer
                    Case DQDataTypes.Integer
                        integerParam.Value = uib.Answer
                    Case DQDataTypes.Double
                        doubleParam.Value = uib.Answer
                    Case DQDataTypes.Date
                        dateParam.Value = uib.Answer
                    Case DQDataTypes.Currency
                        currencyParam.Value = uib.Answer
                    Case DQDataTypes.Boolean
                        booleanParam.Value = uib.Answer
                    Case DQDataTypes.Option
                        optionParam.Value = uib.Answer
                End Select

                Dim dynamicQuestionId As Integer = Convert.ToInt32(c.ID.Substring(2))
                SqlHelper.ExecuteReader(ConfigurationSettings.AppSettings("connectionString"), _
                                            CommandType.StoredProcedure, "dq_AddDynamicAnswer", _
                                            New SqlParameter("@CustomerID", _customerId), _
                                            New SqlParameter("@DynamicQuestionID", dynamicQuestionId), _
                                            stringParam, integerParam, doubleParam, _
                                            dateParam, currencyParam, booleanParam, optionParam)
            End If
        Next

        Response.Redirect("Default.aspx")   'Send the user back to the main page
    End Sub
End Class
